﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Xml;

namespace Clandestine.Conversations
{
    public class QuestionPart : ConversationPart
    {
        public string Text = string.Empty;
        public string[] Options;
        public ConversationPart[] OptionsNext;

        private int selectedOption = -1;

        public override ConversationPart NextPart
        {
            get
            {
                return OptionsNext[selectedOption];
            }
            set { throw new Exception("Malformed XML me thinks."); }
        }

        public QuestionPart()
        { }

        public override void Do()
        {
            // Create the menu items
            List<MenuItem> menuItems = new List<MenuItem>();
            for (int i = 0; i < Options.Length; i++)
                menuItems.Add(new MenuItem(Options[i], i, delegate(MenuItem m)
                {
                    selectedOption = (int)m.Object;
                }));
            
            // Setup the menu
            Menu menuOpt = new Menu(new Size(139, 95), Alignment.Right, Window.MenuPosition, menuItems.ToArray());
            menuOpt.Enabled = true;
            menuOpt.GluedToCamera = true;
            menuOpt.Resize();

            // Show the text on screen
            TextSprite sprText = Fonts.Default2.DrawText(Text);
            sprText.Position = Window.SpeechPosition;
            sprText.Layer = Window.Layer + DialogueWindow.LayerDifference;
            sprText.GluedToCamera = true;

            // Wait for them to select an option
            menuOpt.WaitForMenu();

            // Cleanup
            menuOpt.Dispose();
            sprText.Dispose();
        }

        public override List<ConversationPart> ParseXML(XmlTextReader xml)
        {
            List<string> options = new List<string>();
            List<ConversationPart> optionsNext = new List<ConversationPart>();
            List<ConversationPart> AwaitingPointers = new List<ConversationPart>();

            // Loop through and add options! (&& the text!)
            while (!((xml.NodeType == XmlNodeType.EndElement) && (xml.Name == "part")) && xml.Read())
            {
                // Option
                if ((xml.NodeType == XmlNodeType.Element) && (xml.Name == "option"))
                {
                    // Get text
                    options.Add(xml.GetAttribute("text"));
                    
                    // Parse parts
                    ConversationPart optStart = null;
                    List<ConversationPart> buffAwaitingPointers = Conversation.ParseXML(this.Conversation, xml, "option", out optStart);
                    optionsNext.Add(optStart);

                    // buffAwaitingPointers -> AwaitingPointers
                    AwaitingPointers.AddRange(buffAwaitingPointers);
                }

                // Text -- if multiple <text> tags, take the latest.
                if ((xml.NodeType == XmlNodeType.Element) && (xml.Name == "text"))
                    this.Text = xml.ReadElementContentAsString();
            }

            this.Options = options.ToArray();
            this.OptionsNext = optionsNext.ToArray();
            return AwaitingPointers;
        }
    }
}
